package cn.knightzz.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author knightzz
 * @date 2021/4/7 22:02
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    public static final Logger logger = LoggerFactory.getLogger(MyMetaObjectHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        logger.info("start inset fill = = = = = = = >");
        // 自动填充数据
        // 插入的时候更新数据
        this.setFieldValByName("createTime", new Date(), metaObject);
        // 插入和更新的时候更新数据
        this.setFieldValByName("updateTime", new Date(), metaObject);
        // 插入的时候自动添加
        this.setFieldValByName("enabled", 1, metaObject);

        // 插入时添加版本值
        //this.setFieldValByName("version", 1, metaObject);
        // 插入时添加默认的deleted值 0是未被删除 1是删除
        //this.setFieldValByName("deleted", 0, metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        logger.info("start update fill = = = = = = = >");
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }
}